<?php

$tabBoxPages = array();

class TabBox
{
    var $boxid;
    var $menus = array();

    public function __construct($id = "ttbox")
    {
        $this->boxid = empty($id) ? "ttbox" : $id;
    }

    public function addMenu($id, $name, $url)
    {
        $menu = array("id" => $id, "name" => $name, "url" => $url, "focus" => false);
        array_push($this->menus, $menu);
    }

    public function checkFocus()
    {
        $name = "button_" . $this->boxid;
        $focusId = deep_get($name);

        $find = false;
        for ($i = 0; $i < count($this->menus); $i++) {
            $menu = &$this->menus[$i];
            $menu["focus"] = ($menu["id"] == $focusId);
            if ($menu["id"] == $focusId) {
                $find = true;
            }
        }
        if (!$find) {
            $this->menus[0]["focus"] = true;
        }
    }

    public function showHtmlHead()
    {
        echo $this->getHtmlHead();
    }

    public function show_html_head()
    {
        echo $this->get_html_head();
    }

    public function showHtmlFoot()
    {
        echo $this->getHtmlFoot();
    }

    public function getHtmlHead()
    {
        global $tabBoxPages;
        global $_SERVER;

        $crlf = "\r\n";
        $tab = "\t";

        $page = $_SERVER["SCRIPT_NAME"];
        $html = "";
        if (!in_array($page, $tabBoxPages)) {
            array_push($tabBoxPages, $page);
            $html .= $this->getJsCss();
        }

        $this->checkFocus();

        $boxid = $this->boxid;
        $html .= "<aazone id='aatabs_$boxid' class='aatabs' name='$boxid'>";

        $src = '';
        for ($i = 0; $i < count($this->menus); $i++) {
            $menu = $this->menus[$i];
            $id = $menu["id"];
            $css = $menu["focus"] ? "ctfocus" : "ctdark";
            $url = $menu["url"];
            $divId = $this->boxid . "_col_" . $i;
            $html .= "<div id='$divId' class='coltab $css' tabid='" . $id . "' url='$url'> $crlf";
            $html .= $menu["name"] . " $crlf";
            $html .= "</div> $crlf";
        }
        $html .= "<div class='coltab_innerdiv'> $crlf";

        return $html;
    }

    public function get_html_head()
    {
        return $html . $this->getHtmlHead();
    }

    public function getHtmlFoot()
    {
        $crlf = "\r\n";
        $tab = "\t";

        $html = "";
        $html .= "</div> $crlf";
        $html .= "</aazone> $crlf";
        return $html;
    }

    public function getJsCss()
    {
        return "
			<link rel='stylesheet' href='/p5/components/tabbox.css?a=2'></link>
			<script language='JavaScript' src='/p5/components/tabbox.js?a=2'></script>
		";
    }

    public function getUrl()
    {
        $this->createUrlFiles();
        for ($i = 0; $i < count($this->menus); $i++) {
            $menu = $this->menus[$i];
            if ($menu["focus"]) {
                return $menu["url"];
            }
        }
        return null;
    }

    /**
     * 创建url对应的文件（如果不存在）
     */
    public function createUrlFiles()
    {
        for ($i = 0; $i < count($this->menus); $i++) {
            $menu = $this->menus[$i];
            $url = $menu["url"];
            $matrix = explode("?", $url);
            $url = $matrix[0];
            if (file_exists($url)) {
                continue;
            }
            FileUtils::rewrite_file($url, '<?php ' . "\n\n" . 'require_once("../../common/include/null.php");');
        }
    }

}
